/*
 * ItemRentDAOImpl.java
 *
 * Created on July 7, 2007, 2:58 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.abbh.libcommunity.dao.impl;

import com.abbh.libcommunity.dao.LCHibernateUtil;
import com.abbh.libcommunity.model.Item;
import com.abbh.libcommunity.model.User;
import com.abbh.libcommunity.model.UserRentedItems;
import com.abbh.libcommunity.dao.ItemRentDAO;
import com.abbh.libcommunity.model.UserHoldItems;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.Query;


/**
 *
 * @author PAUL
 */
public class ItemRentDAOImpl implements ItemRentDAO {
    
    Session s;
    Transaction t;
    Query q;
    List l;
    
    /** Creates a new instance of ItemRentDAOImpl */
    public ItemRentDAOImpl() {
        s=com.abbh.libcommunity.dao.LCHibernateUtil.currentSession();
        t=s.beginTransaction();
        q=null;
        l=null;
    }
    public void rent(User user,Item item){
        UserRentedItems uri=new UserRentedItems();
        
        
        uri.setLastModifiedUser("abdul");
        uri.setItem(item);
        uri.setUser(user);
        uri.setLastModifiedUser("none");
        s.save(uri);
        t.commit();
        
    }
    
    public  void renew(UserRentedItems urItem){
        
        s.save(urItem);
        t.commit();
    }
    public  void returns(UserHoldItems uhItem){
        s.save(uhItem);
        t.commit();
    }
    
    public  UserRentedItems  findByItemId(Integer  id) {
        
        q = s.createQuery("select r from UserRentedItems r ");
        System.out.println ( q.list());
        UserRentedItems u = (UserRentedItems) q.list().get(0);
        return u;
    }
    
    public void update(UserRentedItems userRentedItems) {
        Session session = null;
        Transaction tx = null;
        try {
            session = LCHibernateUtil.currentSession();
            tx = session.beginTransaction();
            session.update(userRentedItems);
            tx.commit();
        } catch ( RuntimeException re) {
            tx.rollback();
            re.printStackTrace();
        } finally {
            session.close();
        }
        
    }
}
